package me.sunpeng.channel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.TimeUnit;

/**
 * @author sp
 */
public class FileChannelDemo {
    final static String PATH = "iofile\\1.txt";
    final static String SRC_PATH = "iofile\\src.txt";
    final static String DST_PATH = "iofile\\dst.txt";

    public static void main(String[] args) throws IOException, InterruptedException {
//        transferFrom();
//        transferTo();

        Thread a = new Thread(() -> {
            System.out.println("进入a线程 " + Thread.currentThread().getName());
            new Thread(() -> {
                System.out.println("a1");
            }, "a1").start();
            new Thread(() -> {
                System.out.println("a2");
            }, "a2").start();
            try {
                TimeUnit.MILLISECONDS.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "a");
        a.start();
        a.join();
        System.out.println("main end");

    }

    public static void transferTo() throws IOException {
        RandomAccessFile aFile = new
                RandomAccessFile(SRC_PATH, "rw");
        FileChannel fromChannel = aFile.getChannel();

        RandomAccessFile bFile = new
                RandomAccessFile(DST_PATH, "rw");
        FileChannel toChannel = bFile.getChannel();

        long position = 0;
        long count = fromChannel.size();
        fromChannel.transferTo(position, count, toChannel);
        aFile.close();
        bFile.close();
        System.out.println("over!");

    }


    public static void transferFrom() throws IOException {
        RandomAccessFile aFile = new
                RandomAccessFile(SRC_PATH, "rw");
        FileChannel fromChannel = aFile.getChannel();

        RandomAccessFile bFile = new
                RandomAccessFile(DST_PATH, "rw");
        FileChannel toChannel = bFile.getChannel();

        long position = 0;
        long count = fromChannel.size();

        toChannel.transferFrom(fromChannel, position, count);

        aFile.close();
        bFile.close();
        System.out.println("over!");
    }


    /**
     * 向 FileChannel 写数据
     *
     * @param args
     * @throws IOException
     */
    public static void write(String[] args) throws IOException {

        try (RandomAccessFile accessFile = new RandomAccessFile(PATH, "rw");
             //打开一个Channel
             FileChannel inChannel = accessFile.getChannel()) {
            String newData = "New String to write to file..." +
                    System.currentTimeMillis();
            //创建一个buff
            ByteBuffer buf1 = ByteBuffer.allocate(48);
            buf1.clear();

            //写入内容
            buf1.put(newData.getBytes());
            buf1.flip();
            while (buf1.hasRemaining()) {
                inChannel.write(buf1);
            }
        }

    }

    /**
     * /FileChannel读取数据到buffer中
     */
    public static void read(String[] args) throws IOException {

        try (RandomAccessFile accessFile = new RandomAccessFile(PATH, "rw");
             //创建FileChannel
             FileChannel channel = accessFile.getChannel()) {
            //创建Buffer
            ByteBuffer buf = ByteBuffer.allocate(1024);
            //读数据到buffer中
            int byteRead = channel.read(buf);
            while (byteRead != -1) {
                System.out.println("读取了：" + byteRead);
                buf.flip();
                while (buf.hasRemaining()) {
                    System.out.println((char) buf.get());
                }
                buf.clear();
                byteRead = channel.read(buf);
            }

        }

    }
}
